//
//  SYOriginWioboView.swift
//  SYWeibo
//
//  Created by Saya on 16/4/12.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class SYOriginWioboView: UIView {
    //用于记录自己底部的约束
    private var tempConstraint:Constraint?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //数据模型
    var statusModel:SYCellStatusViewModel?{
        //给各个控件设置赋值
        didSet{
            iconImg.sd_setImageWithURL(statusModel?.userIconURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLB.text = statusModel?.status?.user?.name
            mbRankImageView.image = statusModel?.mbrankImg
            verifiedImageView.image = statusModel?.verifiedImg
            timeLb.text = statusModel?.status?.created_at
            sourceLb.text = statusModel?.source
            contentLb.text = statusModel?.status?.text
            
            guard let tempModel = statusModel else {
                print("数据错误")
                return
            }
            calSelfBottomConstraint(tempModel)
        }
    }
    
    private func calSelfBottomConstraint(statusModel:SYCellStatusViewModel){
        self.tempConstraint?.uninstall()
        if statusModel.status?.pic_urls?.count <= 0 {
            //该微博没有图片
            //更行约束
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.tempConstraint = make.bottom.equalTo(contentLb.snp_bottom).constraint
            })
            //设置图片view为隐藏
            pictureView.hidden = true
            //将图片view的数据设置为nil
            pictureView.pic_urls = nil
        }else{
            //该微博有图片
            //更行约束
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.tempConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
            })
            //设置图片view为显示
            pictureView.hidden = false
            //设置数据
            pictureView.pic_urls = statusModel.status?.pic_urls
        }
    }
    
    private func setUpUI(){
        
        //添加分割线
//        addSubview(seperaterView)
        //添加子控件
        addSubview(iconImg)
        addSubview(nameLB)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        addSubview(timeLb)
        addSubview(sourceLb)
        addSubview(contentLb)
        addSubview(pictureView)
        
        
        //设置约束
//        seperaterView.snp_makeConstraints { (make) -> Void in
//            make.top.left.right.equalTo(self)
//            make.height.equalTo(marginForCell)
//        }
        iconImg.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(marginForCell)
            make.left.equalTo(self.snp_left).offset(marginForCell)
            make.width.equalTo(44)
            make.height.equalTo(44)
        }
        nameLB.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImg.snp_top)
            make.left.equalTo(iconImg.snp_right).offset(marginForCell)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLB.snp_top)
            make.left.equalTo(nameLB.snp_right).offset(marginForCell)
        }
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconImg.snp_right)
            make.bottom.equalTo(iconImg.snp_bottom)
        }
        timeLb.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImg.snp_right).offset(marginForCell)
            make.bottom.equalTo(iconImg.snp_bottom)        }
        
        sourceLb.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLb.snp_right).offset(marginForCell)
            make.bottom.equalTo(timeLb.snp_bottom)
        }
        contentLb.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImg.snp_bottom).offset(marginForCell)
            make.left.right.equalTo(self).offset(marginForCell)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLb.snp_bottom).offset(marginForCell)
            make.left.equalTo(self.snp_left).offset(marginForCell)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.tempConstraint = make.bottom.equalTo(contentLb.snp_bottom).offset(marginForCell).constraint
        }
    }
    
    //懒加载控件
    
    lazy var iconImg:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    lazy var nameLB:UILabel = UILabel(text: "sayaHuang", font: 15, textColor: UIColor.darkGrayColor())
    lazy var mbRankImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    lazy var sourceLb:UILabel = UILabel(text: "saya15", font: 12, textColor: UIColor.darkGrayColor())
    lazy var timeLb:UILabel = UILabel(text: "66:66", font: 12, textColor: UIColor.orangeColor())
    lazy var contentLb:UILabel = UILabel(text: "666.saya大程序啊啦拉啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦拉拉拉拉啦啦啦啦啦666.saya大程序啊啦拉啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦拉拉拉拉啦啦啦啦啦", font: 14, textColor: UIColor.darkGrayColor(), margin: marginForCell)
    
    //添加一个uiview用于分割cell
    lazy var seperaterView:UIView = {
        let tempView = UIView()
        tempView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        return tempView
    }()
    
    //创建一个view用于放置图片
    lazy var pictureView:SYPictureView = SYPictureView()
}
